<template>
  <div>
    <!-- 左侧菜单 -->
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>名称：</label>
            <el-input clearable placeholder="请输入名称" type="text" v-model="keyword"></el-input>
          </div>
          <div class="operate_item">
            <label>电话：</label>
            <el-input clearable placeholder="请输入电话" type="text" v-model="phone"></el-input>
          </div>
          <div class="operate_item">
            <span @click="getTableInfo(1)" class="operate_btn search_btn">查询</span>
            <span @click="resetTable" class="operate_btn reset_btn">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span @click="openPopup('')" class="operate_btn search_btn" >新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          :data="tableInfo"
          :header-cell-style="tableHeader"
          border
          ref="multipleTable"
          style="width: 100%;"
          tooltip-effect="dark"
          v-loading="loading">
          <el-table-column
            label="ID"
            prop="id"
            width="80">
          </el-table-column>
          <el-table-column
            label="更新时间"
            prop="modifyTime"
            sortable
            width="160">
            <template slot-scope="scope">
              <p style="text-align: left">{{ formatTime(scope.row.modifyTime) }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="负责仓库名">
            <template slot-scope="scope">
              <span>{{ scope.row.warehouseName ? scope.row.warehouseName : '——' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="名称"
            prop="name">
          </el-table-column>
          <el-table-column
            label="联系电话">
            <template slot-scope="scope">
              <span>{{ scope.row.phone ? scope.row.phone : '——' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="状态"
            width="100">
            <template slot-scope="scope">
                    <span :class="{'unusual':scope.row.enable===0}"
                          class="normal">{{ scope.row.enable ? '可用' : '禁用' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            v-if="btn&&(btn.update||btn.delete)"
            width="150">
            <template slot-scope="scope">
              <div class="table_btn">
                <span @click="openPopup(scope.row.id)" class="change_info" v-if="btn.update">编辑</span>
                <span @click="deleteItem(scope.row)" class="change_info delete_btn" v-if="btn.delete">删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[15, 20, 30, 40]"
          :total="totals"
          @current-change="getTableInfo"
          @size-change="sizeChange"
          background
          layout="prev, pager, next, sizes,jumper">
        </el-pagination>
      </div>
    </div>
    <edit :mid="menuId" :isOther="isOther" @listenToChild="listenToEdit" v-if="isPop"></edit>
  </div>
</template>

<script>
  import edit from "./edit";
  import commons from "@/utils/common";
  import {getBtn} from "@/utils/btnAuth";
  import {getManager,delManager} from "@/api/basic/warehouse/manager";

  export default {
    name: 'management',
    components: {
      edit
    },
    data() {
      return {
        keyword: '',
        phone: '',
        currentPage: 1,
        pageSize: 15,
        totals: 0,
        loading: false,
        tableInfo: [],
        menuId: '',
        isPop: false,
        btn: {},
        isOther:''
      }
    },
    mounted() {
      if(this.$route.query.id) {
        this.menuId = parseInt(this.$route.query.id);
        this.isPop = true;
        this.isOther = true;
      }
      this.btn = getBtn();
      if (this.btn.query) {
        this.getTableInfo(1);
      }
    },
    methods: {
      getTableInfo(page) {
        let that = this;
        if(!that.$route.query.id) {
          that.loading = true;
        }
        that.currentPage = page;
        let queryParams = {
          name:that.keyword,
          phone:that.phone,
          limit: that.pageSize,
          page: page
        };
        getManager(queryParams).then(res => {
          that.tableInfo = res.list ? res.list : [];
          that.totals = res.count;
          that.loading = false;
        }).catch((e) => {
          that.tableInfo = [];
          that.totals = 0;
          that.loading = false;
        });
      },

      resetTable() {
        this.keyword = '';
        this.address = '';
        this.phone = '';
        this.pageSize = 15;
        this.getTableInfo(1)
      },

      // 移除
      deleteItem(obj) {
        let that = this;
        that.$confirm('确定删除「' + obj.name + '」仓管?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delManager(obj.id).then(res => {
            that.getTableInfo(that.currentPage);
            that.$message.success({
              message:'删除成功！',
              duration:1500
            });
          }).catch((e) => {

          });
        }).catch(() => {
        });
      },

      // 打开弹窗
      openPopup(id) {
        this.isOther = '';
        this.isPop = true;
        this.menuId = id;
      },

      listenToEdit(e) {
        this.isPop = false;
        if (e.refresh) {
          this.getTableInfo(this.currentPage)
        }
      },

      formatTime(time) {
        return commons.formatTime(time)
      },

      sizeChange(val) {
        this.pageSize = val;
        this.getTableInfo(1)
      },

      // 设置table标题居中
      tableHeader({row, column, rowIndex, columnIndex}) {
        return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600;'
      },
    }
  }
</script>

<style scoped>

</style>
